package com.meichis.yndms.repository.manager;

import android.util.ArrayMap;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.http.CallSubscriber;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.http.RetrofitUtil.RxSchedulersHelper;
import com.meichis.yndms.model.DailySales;
import com.meichis.yndms.repository.ApiUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xmy
 * @title
 * @remake
 * @create 2021-04-14 22:51
 */
public class DailySalesManager extends BaseManager {

    private static DailySalesManager INSTANCE;

    public static DailySalesManager getInstance() {
        if (INSTANCE == null) {
            synchronized (DailySalesManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DailySalesManager();
                }
            }
        }
        return INSTANCE;
    }

    public void GetUserSalesDayKanban(final MutableLiveData<Boolean> showDialog, String IBMDealerID,   MutableLiveData<List<DailySales>> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("IBMDealerID", IBMDealerID);
        getRootApi().GetUserSalesDayKanban(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<DailySales>>() {
                }.getType(), new IWebServiceCallback<List<DailySales>>() {
                    @Override
                    public void onSucceed(int sequence,List<DailySales> resultPack, String ReturnInfo, int Return) {
//                        String str = "[{\"UserName\":\"十三\",\"UserSalesMoney\":1644.65,\"TerminalNum\":4.0,\"ProductSaleList\":[{\"ProductType\":\"QQ星有机奶\",\"ProductSalesMoney\":250.0},{\"ProductType\":\"安慕希\",\"ProductSalesMoney\":483.15},{\"ProductType\":\"儿童花色奶\",\"ProductSalesMoney\":125.0},{\"ProductType\":\"谷粒多\",\"ProductSalesMoney\":96.0},{\"ProductType\":\"金典\",\"ProductSalesMoney\":464.0},{\"ProductType\":\"基础优酸乳\",\"ProductSalesMoney\":85.0},{\"ProductType\":\"味可滋\",\"ProductSalesMoney\":27.0},{\"ProductType\":\"砖优酸乳\",\"ProductSalesMoney\":114.5}]},{\"UserName\":\"苗淼淼\",\"UserSalesMoney\":1598.98,\"TerminalNum\":2.0,\"ProductSaleList\":[{\"ProductType\":\"安慕希\",\"ProductSalesMoney\":253.98},{\"ProductType\":\"儿童成长奶\",\"ProductSalesMoney\":150.0},{\"ProductType\":\"谷粒多\",\"ProductSalesMoney\":240.0},{\"ProductType\":\"基础优酸乳\",\"ProductSalesMoney\":265.0},{\"ProductType\":\"金典\",\"ProductSalesMoney\":522.0},{\"ProductType\":\"味可滋\",\"ProductSalesMoney\":168.0}]}]";
//                        resultPack =new Gson().fromJson(str,new TypeToken<List<DailySales>>(){}.getType());
                        if (resultPack == null) {
                            MessageTools.showLongToast("暂无数据！");
                            data.setValue(new ArrayList<>());
                        } else {
                            data.setValue(resultPack);
                        }
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence,int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));

    }

}
